﻿using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;

namespace WinFormsApp1.Leo.ListWeeks03
{
    public partial class SaleAdd : Form
    {
        public SaleAdd()
        {
            InitializeComponent();
            GetCheck();
            GetUPCData();
            GetProduct();
            GetDepot();
        }

        //进场编号下拉
        public async void GetCheck()
        {
            CheckIn supplier = new();

            var items = await HttpClientHelper.RequestUrl<CheckIn, ApiResult<List<CheckIn>>>(URLCommon.Url2, "/CheckIn/CheckIn2Show", HttpClientHelper.MethodType.POST, supplier);

            comboBox1.DataSource = items.Data;
            comboBox1.DisplayMember = "CheckInNo";
            comboBox1.ValueMember = "CheckInId";
        }

        public class CheckIn
        {
            public int CheckInId { get; set; }
            public string CheckInNo { get; set; }
        }


        //商品代号
        public async void GetUPCData()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<UPCDataser>>>(URLCommon.Url2, "/WeekListOne_Leo/GetUPCDataShow", HttpClientHelper.MethodType.GET, default);

            comboBox2.DataSource = items.Data;
            comboBox2.DisplayMember = "UPCDataName";
            comboBox2.ValueMember = "GoodsMinuteId";

            this.Controls.Add(comboBox3);
        }

        public class UPCDataser
        {
            public int GoodsMinuteId { get; set; }
            public string UPCDataName { get; set; }
        }

        //仓库
        public async void GetDepot()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<Depotes>>>(URLCommon.Url2, "/Tsw/GetDepottypeData", HttpClientHelper.MethodType.GET, default);

            comboBox4.DataSource = items.Data;
            comboBox4.DisplayMember = "DepotTypeName";
            comboBox4.ValueMember = "DepotTypeId";

            this.Controls.Add(comboBox3);
        }

        public class Depotes
        {
            public int DepotTypeId { get; set; }
            public string DepotTypeName { get; set; }
        }

        //商品批号
        public async void GetProduct()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<Productser>>>(URLCommon.Url2, "/WeekListOne_Leo/GetProductShow", HttpClientHelper.MethodType.GET, default);

            comboBox3.DataSource = items.Data;
            comboBox3.DisplayMember = "ProductName";
            comboBox3.ValueMember = "ProductId";

            this.Controls.Add(comboBox3);
        }

        public class Productser
        {
            public int ProductId { get; set; }
            /// <summary>
            /// 商品批号名称
            /// </summary>
            public string ProductName { get; set; }
        }


        public record Salerest(int CheckInId, int MarkId, string SaleNum, string SaleGrossWeight, string SaleTares, string SaleWeight,
           int ProductId, string SaleRemark, int DepotId, DateTime SaleTime, bool IsDelete);

        private async void button1_Click_1(object sender, EventArgs e)
        {
            var CheckInId = Convert.ToInt32(comboBox1.SelectedValue);
            var MarkId = Convert.ToInt32(comboBox2.SelectedValue);
            var SaleNum = textBox1.Text;
            var SaleGrossWeight = textBox2.Text;
            var SaleTares = textBox3.Text;
            var SaleWeight = textBox4.Text;
            var ProductId = Convert.ToInt32(comboBox3.SelectedValue);
            var SaleRemark = textBox5.Text;
            var DepotId = Convert.ToInt32(comboBox4.SelectedValue);
            var SaleTime = Convert.ToDateTime(dateTimePicker1.Text);
            var IsDelete = false;



            Salerest clients = new(CheckInId, MarkId, SaleNum, SaleGrossWeight, SaleTares, SaleWeight, ProductId, SaleRemark, DepotId, SaleTime, IsDelete);
            var res = await HttpClientHelper.RequestUrl<Salerest, ApiResult<Saleres>>(URLCommon.Url1, "/WorkList01_leo/GetSaleAdd", HttpClientHelper.MethodType.POST, clients);
            if (res.Code == 1)
            {
                MessageBox.Show("添加成功");
            }
        }

        public class Saleres
        {
            public int SaleId { get; set; }

            /// <summary>
            /// 进场编号
            /// </summary>
            public int CheckInId { get; set; }

            /// <summary>
            /// 商品代号
            /// </summary>
            public int MarkId { get; set; }

            /// <summary>
            /// 件数
            /// </summary>
            public string SaleNum { get; set; }

            /// <summary>
            /// 毛重
            /// </summary>
            public string SaleGrossWeight { get; set; }

            /// <summary>
            /// 皮重
            /// </summary>
            public string SaleTares { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string SaleWeight { get; set; }

            /// <summary>
            /// 商品批号
            /// </summary>
            public int ProductId { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string SaleRemark { get; set; }

            /// <summary>
            /// 仓库
            /// </summary>
            public int DepotId { get; set; }

            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime? SaleTime { get; set; }

            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; } = false;
        }

        
    }
}
